#!/bin/sh
# postinst script for freedomotic
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
          # If the package has default file it could be sourced, so that
       # the local admin can overwrite the defaults

       [ -f "/etc/default/freedomotic" ] && . /etc/default/freedomotic

       # Sane defaults:

       [ -z "$SERVER_HOME" ] && SERVER_HOME=/etc/freedomotic
       [ -z "$SERVER_USER" ] && SERVER_USER=freedomotic
       [ -z "$SERVER_NAME" ] && SERVER_NAME="Freedomotic"
       [ -z "$SERVER_GROUP" ] && SERVER_GROUP=freedomotic
#  Create correct dir and link for the librxtx-java.so
	RXTXLIB=/usr/lib/jni/librxtxSerial.so
	ARCHDIR=/etc/$SERVER_USER/serial/linux
	DESTDIR=none
	case $(uname -i) in
	i386)
		DESTDIR=i386
	;;
	i686)
		DESTDIR=i386
	;;
	arm)
		DESTDIR=arm
	;;
	amd64)
		DESTDIR=amd64
	;;
	x86_64)
		DESTDIR=x86
	;;
	unknown)
		case $(uname -m) in
		armv6l)
			DESTDIR=arm
		;;
		x86_64)
			DESTDIR=x86
		;;
		*)
			echo "ERROR: UNIDENTIFIED ARCHITECTURE FOR `uname -a`"
		;;
		esac
	;;
	*)
		echo "ERROR: DON'T KNOW WHAT TO DO WITH ARCHITECTURE `arch`"
	;;
	esac

	if [ DESTDIR != "none" ]
	then
		mkdir -p $ARCHDIR/$DESTDIR
		ln -sf $RXTXLIB $ARCHDIR/$DESTDIR/
	fi
        echo "\n## WARNING ## "
        echo "## Remember to add users to 'freedomotic' group prior to use Freedomotic"
        echo "## or you won't be able to save your changes!"
        echo "## Use 'sudo adduser <username> freedomotic', and let involved user(s) re-login."
	echo "## WARNING ## \n"
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
